require 'digest/sha1'
class Paper < ActiveRecord::Base
   has_many :comments
   has_many :p_keywords,:through=>:pk_relations
   has_many :pk_relations
   validates_presence_of   :title,:author1,:publisher,:ISBN,:page_info,:abstract,:uploader_summary,:keyword1
   validates_uniqueness_of   :title,:ISBN
   validates_presence_of :originalfile,:message=>"Your should select the orignal file to upload!!",:if=>Proc.new {|paper| paper.save_url.blank? }
GENERIC_ANALYSIS_REGEX = /([a-zA-Z]|[\xc0-\xdf][\x80-\xbf])+|[0-9]+|[\xe0-\xef][\x80-\xbf][\x80-\xbf]/
    GENERIC_ANALYZER = Ferret::Analysis::RegExpAnalyzer.new(GENERIC_ANALYSIS_REGEX, true)
   acts_as_ferret({:fields => [:paper_category_id,:title,:author1,:author2,:author3,:author4,:author5,
              :publisher,:keyword1,:keyword2,:keyword3,:keyword4,:keyword5,:keyword6,:abstract,:uploader_summary]},{ :analyzer => GENERIC_ANALYZER })


  def self.full_text_search(q, options = {})
   return nil if q.nil? or q==""
   default_options = {:limit => 10, :page => 1}
   options = default_options.merge options
   
   # get the offset based on what page we're on
   options[:offset] = options[:limit] * (options.delete(:page).to_i-1)  
   
   # now do the query with our options
   results = Paper.find_by_contents(q, options)
   return [results.total_hits, results]
end
def add_paper_keywords
      if !id.blank?
        for keyword in keywords
          PkRelation.add_reference(id,keyword)
        end
      end
end
def edit_paper_keywords(oldkeywords)      
    PkRelation.alter_relation(id,oldkeywords,keywords)
end
 def self.toppapers
   toppapers=Paper.find(:all,:order => "viewcount DESC", :limit=>10)
   return toppapers
 end
 
 def self.recentpapers
   recentpapers=Paper.find(:all,:order=> "upload_time DESC", :limit=>10)
   return recentpapers
 end
 def delete_keywords_relation
   PkRelation.destroy_all ["paper_id=?",id]
   for kword in keywords
        PKeyword.sub_ref_count(kword)
   end
 end
 def authors
   authors=[author1]
   authors.push(author2) if !author2.blank?
   authors.push(author3) if !author3.blank?
   authors.push(author4) if !author4.blank?
   authors.push(author5) if !author5.blank?
   return authors
 end
 def keywords
   keywords=[keyword1]
   keywords.push(keyword2) if !keyword2.blank?
   keywords.push(keyword3) if !keyword3.blank?
   keywords.push(keyword4) if !keyword4.blank?
   keywords.push(keyword5) if !keyword5.blank?
   keywords.push(keyword6) if !keyword6.blank?
   return keywords
 end
 
 def time_from_now_str
   time_str=nil
   if !upload_time.blank?
     seconds_val=Time.now-upload_time
     days_val=seconds_val.div(86400)
     hours_val=(seconds_val%86400).div(3600)
     minutes_val=(seconds_val%3600).div(60)
     seconds_val=(seconds_val%60).div(1)
     if days_val>0
       if days_val==1
         time_str=days_val.to_s+" day ago"
       else
         time_str=days_val.to_s+" days ago"
       end       
     elsif hours_val>0
       if hours_val==1
         time_str=hours_val.to_s+" hour ago"
       else
         time_str=hours_val.to_s+" hours ago"
       end       
     elsif minutes_val>0
       if minutes_val==1
         time_str=minutes_val.to_s+" minute ago"
       else
         time_str=minutes_val.to_s+" minutes ago"
       end       
     elsif seconds_val>0
       if seconds_val==1
         time_str=seconds_val.to_s+" second ago"
       else
         time_str=seconds_val.to_s+" seconds ago"
       end       
     else
       time_str="1 second ago"
     end    
   end
   return time_str
 end
 
end